package com.example.thread.pool;

import org.junit.Test;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author readpage
 * @create 2022-11-02 16:46
 */
public class CountNum {
    @Test
    public void test() {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        Future<Integer> submit = executorService.submit(() -> {
            int sum = 0;
            for (int i = 1; i < 10; i++) {
                sum += i;
            }
            return sum;
        });
        Future<Integer> submit1 = executorService.submit(() -> {
            int sum = 0;
            for (int i = 10; i < 20; i++) {
                sum += i;
            }
            return sum;
        });

        try {
            System.out.println(submit.get() + submit1.get());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }
        System.out.println("complete");
    }
}
